package edu.bbu.license.client.android.controller.transfer;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import edu.bbu.license.client.android.controller.BaseController;
import edu.bbu.license.client.android.gui.transfer.FileTransferView;
import edu.bbu.license.client.android.gui.transfer.model.FileTransferItem;
import edu.bbu.license.client.android.services.transfer.listener.TransferListener;
import edu.bbu.license.client.android.services.transfer.model.SimpleTransferObject;

public class TransferController extends BaseController implements TransferListener {

	private volatile FileTransferView fileTransferView = null;
	private static TransferController controllerInstance = null;
	
	public TransferController() {
		getTransferService().addTransferListener(this);
	}
	
	public static synchronized TransferController getInstance() {
		if(controllerInstance == null) {
			controllerInstance = new TransferController();
		}
		
		return controllerInstance;
	}

	public synchronized void atach(FileTransferView fileTrafficView) {
		if(fileTrafficView != null) {			
			this.fileTransferView = fileTrafficView;
			initializeView();
		}
	}

	private void initializeView() {		
		loadTransferItems();
		
		fileTransferView.setTransferItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				fileTransferView.selectItem((FileTransferItem) parent.getItemAtPosition(position));
			}
		});
	}
	
	private void loadTransferItems() {
		List<FileTransferItem> fileTransferItems = new ArrayList<FileTransferItem>();
		
		for (SimpleTransferObject transferObject :  getTransferService().getTransfers()) {
			FileTransferItem transferItem = new FileTransferItem((Context) fileTransferView);

			transferItem.setTransferObject(transferObject);
			transferObject.setFileTransferItem(transferItem);
			
			fileTransferItems.add(transferItem);
		}
		
		fileTransferView.addTransferItems(fileTransferItems);
		
		fileTransferView.setStartButtonListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FileTransferItem selectedItem = fileTransferView.getSelectedItem();
				
				if(selectedItem != null) {
					selectedItem.getTransferObject().startTransfer();
				}
			}
		});
		
		fileTransferView.setPauseButtonListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				FileTransferItem selectedItem = fileTransferView.getSelectedItem();
				
				if(selectedItem != null) {
					selectedItem.getTransferObject().pauseTransfer();
				}
			}
		});

		fileTransferView.setStopButtonListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				FileTransferItem selectedItem = fileTransferView.getSelectedItem();
				
				if(selectedItem != null) {
					selectedItem.getTransferObject().stopTransfer();
				}
			}
		});

		fileTransferView.setRemoveButtonListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				FileTransferItem selectedItem = fileTransferView.getSelectedItem();
				
				if(selectedItem != null) {
					getTransferService().removeTransfer(selectedItem.getTransferObject());
				}
			}
		});
	}

	@Override
	public void onNewTransferStart(SimpleTransferObject newTransferObject) {
		if(fileTransferView != null) {
			FileTransferItem transferItem = new FileTransferItem((Context) fileTransferView);

			transferItem.setTransferObject(newTransferObject);
			newTransferObject.setFileTransferItem(transferItem);
			
			fileTransferView.addTransferItem(transferItem);
		}
	}
	
	@Override
	public void onNewTransferRemove(SimpleTransferObject newTransferObject) {
		fileTransferView.removeTransferItems();
		List<FileTransferItem> fileTransferItems = new ArrayList<FileTransferItem>();
		
		for (SimpleTransferObject transferObject :  getTransferService().getTransfers()) {
			FileTransferItem transferItem = new FileTransferItem((Context) fileTransferView);

			transferItem.setTransferObject(transferObject);
			transferObject.setFileTransferItem(transferItem);
			
			fileTransferItems.add(transferItem);
		}
		
		fileTransferView.addTransferItems(fileTransferItems);
	}
}
